package com.software.shipperagent.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.software.common.CommonUtils;
import com.software.form.bookingform.dao.BookingFormTransaction;
import com.software.form.inquiry.sagent.dao.InquirySagentTransaction;
import com.software.form.inquiryform.dao.InquiryForm;
import com.software.form.inquiryform.dao.InquiryFormTransaction;

public class InquiryFormServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		InquirySagentTransaction ist = (InquirySagentTransaction) CommonUtils.getBean("inquirySagentTransaction");
		String email = (String) request.getSession().getAttribute("user");
		List<String> inos = ist.getInquiriesByEmail(email, 1);
		InquiryFormTransaction ift = (InquiryFormTransaction)CommonUtils.getBean("inquiryformTransaction");
		if(inos != null){
			List list = new ArrayList();
			for(int i = 0; i < inos.size(); i++){
				InquiryForm inf = ift.getInquiry(inos.get(i));
				JSONArray array = new JSONArray();
				array.put(inf.getNo());
				array.put(inf.getShipper());
				array.put(inf.getShipperEmail());
				array.put(inf.getCargoName());
				array.put(inf.getDestination());
				list.add(array);
			}
			JSONObject o = new JSONObject();
			try {
				o.put("inquiries", list);
				CommonUtils.outputJsonData(o.toString(), response);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		JSONObject json = CommonUtils.readJSONStringFromRequestBody(request, response);
		String action = request.getParameter("action");
		if(action.equals("get")){
			InquiryFormTransaction ift = (InquiryFormTransaction)CommonUtils.getBean("inquiryformTransaction");
			try {
				String no = json.getString("no");
				InquiryForm inf = ift.getInquiry(no);
				JSONObject o = new JSONObject();
				o.put("no", inf.getNo());
				o.put("shipper", inf.getShipper());
				o.put("shipperEmail", inf.getShipperEmail());
				o.put("departure", inf.getDeparture());
				o.put("destination", inf.getDestination());
				o.put("cargo", inf.getCargoName());
				o.put("weight", inf.getWeight());
				o.put("volume", inf.getVolume());
				o.put("info", inf.getExtraInfo());
				CommonUtils.outputJsonData(o.toString(), response);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if(action.equals("update")){
			InquirySagentTransaction ist = (InquirySagentTransaction) CommonUtils.getBean("inquirySagentTransaction");
			String email = (String) request.getSession().getAttribute("user");
			try {
				String no = json.getString("no");
				if(ist.updateStatusToDisable(no, email, 0))
					CommonUtils.outputJsonData(1, response);
				else
					CommonUtils.outputJsonData(0, response);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

	
}
